package com.iwinding.ladygg.ui.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.refresh.PullToRefreshBase;
import com.refresh.PullToRefreshBase.OnRefreshListener;
import com.refresh.PullToRefreshListView;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.net.RequestManager;
import com.iwinding.ladygg.net.callback.SosRecordCallback;
import com.iwinding.ladygg.net.request.SosRecordReq;
import com.iwinding.ladygg.net.response.SosRecordRp;
import com.iwinding.ladygg.ui.act.SosDetailActivity;
import com.iwinding.ladygg.ui.adapter.RecordAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的提示
 * Created by hezhiyong on 2017/3/17.
 */

public class MyHintFragment extends BaseFragment implements OnRefreshListener,OnItemClickListener{


    private PullToRefreshListView mListView;

    private RecordAdapter adpater;
    private List<SosRecordRp> list  = new ArrayList<SosRecordRp>();
    private int currentPage = 0;
    private int countSize = 1000;
    private int pageIndex = 10;

    @Override
    public void initView() {
        mListView = (PullToRefreshListView)findViewById(R.id.record_listview);
        mListView.init(mContext);
        mListView.setDividerHeight(10);
        mListView.setDividerDrawable(new ColorDrawable(getResources().getColor(R.color.Color_I)));

        adpater = new RecordAdapter(mContext,list);
        mListView.setAdapter(adpater,false);
        mListView.setOnRefreshListener(this);
        mListView.setOnItemClickListener(this);
        onPullUpToRefresh(mListView);
    }

    @Override
    protected View onCreateRootView() {
        return View.inflate(mContext, R.layout.icane_fragment_record,null);
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        currentPage = 1;
        updateRecordRp(createSosRecordReq(currentPage,pageIndex));
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        mListView.setHasMoreData(true);
        currentPage++;
        updateRecordRp(createSosRecordReq(currentPage,pageIndex));
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SosRecordRp rp = list.get(position);
        Intent intent = new Intent(mContext, SosDetailActivity.class);
        intent.putExtra(SosDetailActivity.SOSID,String.valueOf(rp.sosId));
        startActivity(intent);
    }

    SosRecordReq createSosRecordReq(int currentPage,int pageIndex){
        SosRecordReq req = new SosRecordReq();
        req.type = 2;
        req.from = currentPage;
        req.size = pageIndex;
        return req;
    }

    void updateRecordRp(final SosRecordReq sosRecordReq){
        RequestManager.postSosRecord(mContext, sosRecordReq, new SosRecordCallback() {
            @Override
            public void onResult(List<SosRecordRp> sosRecordRps) {
                //清零
                if(sosRecordReq.from==1){
                    list.clear();
                    countSize = 0;
                }
                if(sosRecordRps!=null && sosRecordRps.size() !=0){
                    list.addAll(sosRecordRps);
                    countSize = list.size();
                }else{
                    mListView.setPullLoadEnabled(false);
                    mListView.setHasMoreData(false);
                }
                adpater.notifyDataSetChanged();
                mListView.setOnRefreshComplete();
            }

            @Override
            public void onFail(String errorMsg) {

            }
        });
    }

}
